<template>
  <div class="getPasswordBox">
    <div class="fromPosition">
      <!--步骤条-->
      <div class="procedure">
        <el-steps :active="procedure" finish-status="success">
          <el-step/>
          <el-step/>
          <el-step/>
        </el-steps>
      </div>
      <!--表单-->
      <div class="formBox">
        <!--第一步-->
        <div class="formItem">
          <el-input v-model.trim="userInfo.account" placeholder="请输入账号" />
        </div>
        <!--第二步-->
        <div class="formItem">
          <div class="title" v-show="userInfo.problem !== ''">
            密保问题：{{userInfo.problem}}
          </div>
          <div class="title" v-show="userInfo.problem === ''">
            该账号未设置密保，无法找回密码。
          </div>
          <el-input v-show="userInfo.problem !== ''" v-model.trim="userInfo.answer" placeholder="请输入密保答案" />
        </div>
        <!--第三步-->
        <div class="formItem">
          <div class="title">
            密码：{{userInfo.password}}
          </div>
        </div>
      </div>
      <div class="btnBox">
        <el-button @click="router.back()">返回</el-button>
        <el-button :disabled="procedure === 0" @click="procedure--">上一步</el-button>
        <el-button :disabled="procedure === 2" @click="submit">下一步</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
  import {ref,reactive,computed} from "vue"
  import {useRouter} from "vue-router"
  import {ElMessage} from "element-plus";

  import {http} from "../config/axios/index.js"

  //路由器对象
  const router = useRouter()

  //当前步骤
  let procedure = ref(0)

  //用户信息
  const userInfo = reactive({
    account: "",
    problem: "",
    answer: "",
    password: ""
  })

  //显示对应步骤页面
  const showFrom = computed(()=>{
    const translate = procedure.value > 0 ? `-${100 / 3 * procedure.value}%` : "0"
    return `translateX(${translate})`
  })

  //步骤处理
  function submit(){
    switch (procedure.value) {
      case 0:
        getProblem()
        break
      case 1:
        getPassword()
        break
    }
  }

  //获取问题
  async function getProblem(){
    if(userInfo.account === ""){
      return ElMessage({
        showClose: true,
        message: "请先输入账号",
        center: true,
      })
    }
    const res = await http({
      method: "POST",
      url: "user/get_problem",
      data: {
        account: userInfo.account
      }
    })
    if(res.data.code === 0){
      procedure.value++
      userInfo.problem = res.data.data.problem || ""
    }else{
      ElMessage({
        showClose: true,
        message: "服务器异常",
        center: true,
      })
    }
  }

  //获取密码
  async function getPassword(){
    if(userInfo.answer === ""){
      return ElMessage({
        showClose: true,
        message: "请先输入问题答案",
        center: true,
      })
    }
    const res = await http({
      method: "POST",
      url: "user/get_password",
      data: {
        account: userInfo.account,
        answer: userInfo.answer
      }
    })
    if(res.data.code === 0){
      if(res.data.data.password){
        procedure.value++
        userInfo.password = res.data.data.password
      }else{
        ElMessage({
          showClose: true,
          message: "回答错误",
          center: true,
        })
      }
    }else{
      ElMessage({
        showClose: true,
        message: "服务器异常",
        center: true,
      })
    }
  }
</script>

<style scoped>
  .getPasswordBox{
    width: 100vw;
    height: 100vh;
    background: #0052D4;  /* fallback for old browsers */
    background: -webkit-linear-gradient(to bottom, #6FB1FC, #4364F7, #0052D4);  /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to bottom, #6FB1FC, #4364F7, #0052D4); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  }
  .fromPosition{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 90vw;
    max-width: 400px;
    height: 70vh;
    border-radius: 10px;
    background-color: var(--el-bg-color);
    overflow: hidden;
  }
  .procedure{
    padding: 15px;
  }
  .formBox{
    width: 300%;
    height: calc(100% - 49px - 62px);
    display: grid;
    grid-template-columns: repeat(3,1fr);
    transition: transform .5s;
    transform: v-bind(showFrom);
  }
  .formItem{
    padding: 15px;
  }
  .formItem .title{
    padding: 15px 0;
    font-weight: bold;
    font-size: 20px;
    text-align: center;
  }
  .btnBox{
    position: absolute;
    bottom: 0;
    padding: 15px;
    width: 100%;
    text-align: center;
  }
</style>